package cn.kgc.crowdfundingadmin02.service;

import cn.kgc.crowdfundingadmin02.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin02.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin02.pojo.Admin;
import cn.kgc.crowdfundingadmin02.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-20 8:57
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
        public Admin login(String loginAcct, String userPswd) {
            //根据loginacct查询admin
            //判断是否为空
            //如果不为空给密码加密
            //和数据库的密码比较
            //如果一样,登录成功
            Admin admin=null;
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria=example.createCriteria();
            criteria.andLoginacctEqualTo(loginAcct);

            List<Admin> list=adminMapper.selectByExample(example);
            if(CrowdFundingUtils.collectionEffective(list)){
                admin=list.get(0);
                String md5 = CrowdFundingUtils.md5(userPswd);
                if (md5.equals(admin.getUserpswd())){
                    //成功
                    return admin;
                }else{
                    //失败
                    admin=null;
                }
            }
            return admin;
        }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        /*设置当前值*/
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        /*keyword没有值的话 查询全部 ,有值根据账号或者邮箱 邮箱地址查询*/
        if(CrowdFundingUtils.stringEffective(keyword)){
        AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1 = example.createCriteria();
            //根据账号查询 模糊
            criteria1.andLoginacctLike("%"+keyword+"%");
            AdminExample.Criteria criteria2 = example.createCriteria();
            /*根据邮箱模糊查询*/
            criteria2.andEmailLike("%"+keyword+"%");
            AdminExample.Criteria criteria3 = example.createCriteria();
            /*根据地址进行模糊查询*/
            criteria3.andUsernameLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);
            List<Admin> list = adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }else{
            /*查询全部   如果值为空的 则查询全部 判断*/
            List<Admin> list = adminMapper.selectByExample(null);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
            AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        //多条数据删除
        //选中的全部删除  在id里面的adminIdList
        criteria.andIdIn(adminIdList);
        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd=admin.getUserpswd();
//        userPswd=CrowdFundingUtils.md5(userPswd);
        userPswd=bCryptPasswordEncoder.encode(userPswd);
        admin.setUserpswd(userPswd);
            //执行保存
        adminMapper.insert(admin);

    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        //对密码进行加密
        if(CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd=admin.getUserpswd();
//            userPswd=CrowdFundingUtils.md5(userPswd);
            userPswd=bCryptPasswordEncoder.encode(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}

